<template>
  <div class="component-admin-groupProd">
    <el-dialog
      v-model="visible"
      :title="$t('groups.viewActivityMerchandise')"
    >
      <!-- 新版规范 -->
      <div class="seckill-list-mod">
        <!-- 搜索栏end -->
        <!-- 表格主体 -->
        <div class="main-container">
          <!-- 表格 -->
          <div class="table-con seckill-table">
            <el-table
              :data="dataList"
              header-cell-class-name="table-header"
              row-class-name="table-row"
              style="width: 100%"
            >
              <el-table-column
                prop="prodName"
                :label="$t('groups.activityProductName')"
              >
                <template #default="scope">
                  <span class="table-cell-text">{{ scope.row.prodName }}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="pic"
                :label="$t('group.proImg')"
              >
                <template #default="scope">
                  <prod-pic
                    height="60"
                    width="60"
                    :pic="scope.row.pic"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="groupProdStatus"
                :label="$t('group.proActStatus')"
              >
                <template #default="scope">
                  <div class="tag-text">
                    {{
                      ["", $t("publics.normal"), $t("groups.expired")][
                        scope.row.groupProdStatus
                      ]
                    }}
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                align="center"
                :label="$t('crud.menu')"
                class=""
              >
                <template #default="scope">
                  <div class="text-btn-con">
                    <div
                      type="text"
                      class="default-btn text-btn"
                      @click="editSkuhandle(scope.row.groupProdId)"
                    >
                      {{ $t('groups.viewActivitySpec') }}
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页 -->
          <el-pagination
            v-if="dataList.length"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="onPageSizeChange"
            @current-change="onPageChange"
          />
        </div>
        <!-- 表格主体end -->
      </div>
    </el-dialog>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />

    <!-- 商品选择弹窗-->
    <prods-select
      v-if="selectGroupProdVisible"
      ref="ProdsSelectRef"
      data-url="/group/prod/getNotGroupProdPage"
      @refresh-select-prods="selectGroupProds"
    />
    <!-- 活动商品规格属性设置 -->
    <GroupProdSetting
      v-if="groupProdSettingVisible"
      ref="GroupProdSettingRef"
    />
  </div>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import GroupProdSetting from './group-prod-sku-setting.vue'
import AddOrUpdate from './groupProd-add-or-update.vue'

const selectGroupProdVisible = ref(false)
const addOrUpdateVisible = ref(false)

onMounted(() => {
  getDataList()
})

const visible = ref(false)
const groupActivityId = ref('')
const init = (groupActivityId) => {
  page.currentPage = 1
  page.pageSize = 10
  page.total = 0
  groupActivityId.value = groupActivityId || null
  visible.value = true
  getDataList()
}
defineExpose({ init })

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
const dataListLoading = ref(false)
const getDataList = (pageParam) => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/platform/group/prod/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize,
          groupActivityId: groupActivityId.value
        }
      )
    )
  })
    .then(({ data }) => {
      dataList.value = data.records
      page.total = data.total
      dataListLoading.value = false
    })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  getDataList(page)
}

const ProdsSelectRef = ref(null)
/**
 * 选择商品回调
 * @param prods
 */
const selectGroupProds = (prods) => {
  if (prods) {
    // 获取所有的商品
    const prodIds = []
    for (let index = 0; index < prods.length; index++) {
      prodIds.push(prods[index].prodId)
    }
    saveGroupProds(prodIds)
  }
}

const { query } = useRoute()
/**
 * 保存活动商品
 * @param prodIds
 */
const saveGroupProds = (prodIds) => {
  http({
    url: http.adornUrl('/group/prod/saveByProdIds'),
    method: 'post',
    data: http.adornData({
      groupActivityId: query.activityId,
      prodIds
    })
  })
    .then(() => {
      ElMessage({
        message: $t('remindPop.success'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList(page)
        }
      })
    })
}

const groupProdSettingVisible = ref(false)
const GroupProdSettingRef = ref(null)
/**
 * 编辑活动sku
 * @param groupProdId
 */
const editSkuhandle = (groupProdId) => {
  groupProdSettingVisible.value = true
  nextTick(() => {
    GroupProdSettingRef.value?.init(groupProdId)
  })
}

/**
 * 每页数量变更
 * @param val
 */
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

/**
 * 页数变更
 * @param val
 */
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}
</script>
